// ///////////////////////////// MIT License //////////////////////////////////// //
//                                                                                //
// Copyright (c) 2010 David Zsolt Manrique                                        //
//                    david.zsolt.manrique@gmail.com                              //
//                                                                                //
// Permission is hereby granted, free of charge, to any person obtaining a copy   //
// of this software and associated documentation files (the "Software"), to deal  //
// in the Software without restriction, including without limitation the rights   //
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell      //
// copies of the Software, and to permit persons to whom the Software is          //
// furnished to do so, subject to the following conditions:                       //
//                                                                                //
// The above copyright notice and this permission notice shall be included in     //
// all copies or substantial portions of the Software.                            //
//                                                                                //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR     //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,       //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE    //
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER         //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  //
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN      //
// THE SOFTWARE.                                                                  //
//                                                                                //
// ////////////////////////////////////////////////////////////////////////////// //

// standard headers
#include <cmdioutil.h>
int main(int argc, char** argv)
{       
try
{
        {
        std::cout << "---pair_expression---" << std::endl;

        std::string e = "2.4,7";
        util::pair_expression<double,int,','> ex;        
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::istringstream is(e);
        std::cout << "stream in : " << bool(is >> ex) << std::endl;
            
        std::cout << std::fixed << std::get<0>(ex) << std::endl;
        std::cout << std::fixed << std::get<1>(ex) << std::endl;

        std::cout << "---pair_expression---" << std::endl;
        }


        {
        std::cout << "---triple_expression---" << std::endl;

        std::string e = "2.4,7,8.1";
        util::triple_expression<int,int,int,','> ex;        
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::istringstream is(e);
        std::cout << "stream in : " << bool(is >> ex) << std::endl;

        std::cout << std::fixed << std::get<0>(ex) << std::endl;
        std::cout << std::fixed << std::get<1>(ex) << std::endl;
        std::cout << std::fixed << std::get<2>(ex) << std::endl;

        std::cout << "---triple_expression---" << std::endl;
        }


        {
        std::cout << "---quad_expression---" << std::endl;

        std::string e = "2,7/8!90";
        util::quad_expression<int,int,int,int,',','/','!'> ex;        
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::istringstream is(e);
        std::cout << "stream in : " << bool(is >> ex) << std::endl;

        std::cout << std::fixed << std::get<0>(ex) << std::endl;
        std::cout << std::fixed << std::get<1>(ex) << std::endl;
        std::cout << std::fixed << std::get<2>(ex) << std::endl;
        std::cout << std::fixed << std::get<3>(ex) << std::endl;

        std::cout << "---quad_expression---" << std::endl;
        }



        {
        std::cout << "---list_expression---" << std::endl;

        std::string e = "1,3,5:9,-2,-1:-5";
        util::list_expression<int> ex;        
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::istringstream is(e);
        
        std::cout << "stream in : " << bool(is >> ex) << std::endl;

        for(util::list_expression<int>::iterator it = ex.begin(); it != ex.end(); it++)
        std::cout << *it << " ";
        std::cout << std::endl;


        std::cout << "---list_expression---" << std::endl;
        }

        {
        std::cout << "---pair of triple_expression---" << std::endl;

        std::string e = "2.4/0.5/0.6,7/8/1";
        util::pair_expression<util::triple_expression<double,double,double,'/'>,util::triple_expression<double,double,double,'/'>> ex;
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::istringstream is(e);
        std::cout << "stream in : " << bool(is >> ex) << std::endl;

        std::cout << std::fixed << std::get<0>(std::get<0>(ex)) << std::endl;
        std::cout << std::fixed << std::get<1>(std::get<0>(ex)) << std::endl;
        std::cout << std::fixed << std::get<2>(std::get<0>(ex)) << std::endl;
        std::cout << std::fixed << std::get<0>(std::get<1>(ex)) << std::endl;
        std::cout << std::fixed << std::get<1>(std::get<1>(ex)) << std::endl;
        std::cout << std::fixed << std::get<2>(std::get<1>(ex)) << std::endl;
//        std::cout << std::fixed << std::get<2>(ex) << std::endl;

        std::cout << "---pair of triple_expression---" << std::endl;
        }

        {
        std::cout << "---match---" << std::endl;

        std::string p = "egy % harom  %  hat ";
        std::string e = "egy  ketto  harom negy  ot   ";

        std::cout << "pattern : \"" << p << "\"" << std::endl; 
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::string s1,s2;
        std::cout << "match : " << util::match(p,e,s1,s2) << std::endl;

        std::cout << "s1 : " << s1 << std::endl;
        std::cout << "s2 : " << s2 << std::endl;


        std::cout << "---match---" << std::endl;
        }

        {
        std::cout << "---match---" << std::endl;

        std::string p = "egy % harom  %  hat +% ";
        std::string e = "egy  ketto  harom negy  hat  het nyoc  ";

        std::cout << "pattern : \"" << p << "\"" << std::endl; 
        std::cout << "expression : \"" << e << "\"" << std::endl; 
        std::cout << "get : " << std::endl; 
        
        std::string s1,s2;
        std::vector<std::string> vs;
        std::cout << "match : " << util::match(p,e,s1,s2,vs) << std::endl;

        std::cout << "s1 : " << s1 << std::endl;
        std::cout << "s2 : " << s2 << std::endl;
        for(int i = 0; i < vs.size(); i++)
            std::cout << "vs["<< i <<"] : " << vs[i] << std::endl;


        std::cout << "---match---" << std::endl;
        }
        
} catch(const char* message)
{
	std::cerr << message << std::endl;
	std::cerr << "(exit)" << std::endl;
	exit(EXIT_FAILURE);
} 
catch(const std::exception & e)
{
        std::cerr << e.what() << std::endl;
        std::cerr << "(exit)" << std::endl;
        exit(EXIT_FAILURE);
}

    return EXIT_SUCCESS;
}


